/*
 * @lc app=leetcode.cn id=494 lang=cpp
 *
 * [494] 目标和
 */

// @lc code=start
class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int sum = accumulate(nums.begin(), nums.end(), 0);
        if (sum < abs(target) || (sum + target) % 2 == 1) return 0;
        sum = (sum + target) / 2;
        vector<int> dp(sum + 1, 0);
        dp[0] = 1;
        for (int& num : nums) {
            for (int i = sum; i >= num; --i) {
                dp[i] += dp[i - num];
            }
        }
        return dp.back();
    }
};
// @lc code=end

